package basic.socket.chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author bingo
 * @Description     聊天室服务端存在的问题:需要为每一个客户端零开辟一条线程，多个socket客户端会出现等待
 * @Date 2018/7/20
 */
public class MultiServer {

    public static void main(String[] args) {
        DataInputStream dis = null;
        DataOutputStream dos = null;
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("服务器已经启动！等待连接。。。。。。");
            while (true) {
                Socket socket = serverSocket.accept();
                dis = new DataInputStream(socket.getInputStream());
                String msg = dis.readUTF();
                 dos = new DataOutputStream(socket.getOutputStream());
                dos.writeUTF("发送消息:" + msg);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
            dis.close();
            dos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
